package com.xiaozhengkeji.mc.plugins.cdkey;

import com.xiaozhengkeji.mc.plugins.cdkey.commands.AdminCommands;
import com.xiaozhengkeji.mc.plugins.cdkey.commands.PlayerCommands;
import com.xiaozhengkeji.mc.plugins.cdkey.hook.PlaceholderAPIHook;
import com.xiaozhengkeji.mc.plugins.cdkey.listener.PlayerListener;
import com.xiaozhengkeji.mc.plugins.cdkey.utils.MsgUtils;
import com.xiaozhengkeji.mc.plugins.cdkey.web.XiaoZhengApiCloud;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

public final class Main extends JavaPlugin {
    public static String PluginsVersion = "1.0.0-bate";
    public static Main main;
    public static String MCVersion;
    public static Boolean dev = false;

    @Override
    public void onLoad() {
        MsgUtils.logServer("StartUpCDKey");
        saveDefaultConfig();
        saveResource("CDKey.db", dev);
        saveResource("CommandsBase.yml", dev);
        saveResource("Base.yml", dev);
        MCVersion = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
        MsgUtils.logServer("EndCDKey");
    }


    @Override
    public void onEnable() {
        main = this;
        MsgUtils.logServer("StartUpCDKey");
        infoSystem();
        registerCommands();
        registerHook();
        registerListener();
        MsgUtils.logServer("EndCDKey");
    }

    @Override
    public void onDisable() {
        MsgUtils.logServer("StartUpCDKey");

        MsgUtils.logServer("EndCDKey");
    }

    void registerHook() {
        PlaceholderAPI.registerPlaceholderHook("CDKey", new PlaceholderAPIHook());
        MsgUtils.logServer("registerHookEnd");
    }

    void registerCommands() {
        getServer().getPluginCommand("cdk").setExecutor(new PlayerCommands());
        getServer().getPluginCommand("cdka").setExecutor(new AdminCommands());
        MsgUtils.logServer("registerCommandsEnd");
    }

    void registerListener() {
        getServer().getPluginManager().registerEvents(new PlayerListener(), this);
        MsgUtils.logServer("registerListenerEnd");
    }

    void loadCore() {

    }

    void infoSystem() {
        MsgUtils.logServer("§a===== §6CDKey > 开始启动 §a=====");
        MsgUtils.logServer("> 欢迎使用CDKey！");
        MsgUtils.logServer("> 已将您的语言设置为：§f中国 §e(zh_CN)");
        MsgUtils.logServer("> MineCraft版本: §f" + MCVersion);
        if (AuthenticationCompatibility()) {
            MsgUtils.logServer("§a支持本插件! ");
        } else {
            MsgUtils.logServer("§4可能不支持本插件!");
        }
        MsgUtils.logServer("> 该游戏版本已开放使用的功能：");
        if (!dev) {
            loadCore();
        }
        MsgUtils.logServer("> 已完成CDKey插件全部功能载入！");
        MsgUtils.logServer("> §a作者 §f小正 §a | 版本: §f" + PluginsVersion);
        MsgUtils.logServer("> §a云端记录最新版本: §4" + XiaoZhengApiCloud.getUPVersion() +
                "\n更新内容:\n" + XiaoZhengApiCloud.getUPData());
        MsgUtils.logServer("> §4定制插件联系QQ: 1419158026 备注(定制)");
        MsgUtils.logServer("§a===== §6CDKey > 启动完成 §a=====");
        MsgUtils.logServer("公告:\n" + XiaoZhengApiCloud.getNotice());
    }

    Boolean AuthenticationCompatibility() {
        switch (MCVersion) {
            case "v1_7_R1":
                return true;
            case "v1_7_R2":
                return true;
            case "v1_7_R3":
                return true;
            case "v1_7_R4":
                return true;
            case "v1_8_R1":
                return true;
            case "v1_8_R2":
                return true;
            case "v1_8_R3":
                return true;
            case "v1_9_R1":
                return true;
            case "v1_9_R2":
                return true;
            case "v1_10_R1":
                return true;
            case "v1_11_R1":
                return true;
            case "v1_12_R1":
                return true;
            case "v1_13_R1":
                return true;
            case "v1_14_R1":
                return true;
            case "v1_15_R1":
                return true;
            case "v1_16_R1":
                return true;
            default:
                return false;
        }
    }

}
